﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ChiTietSanPham : System.Web.UI.Page
{
    public string ma;
    public string so_luong;
    protected void Page_Load(object sender, EventArgs e)
    {
        ma = Request.QueryString["ma"];
        if (ma == null || ma == "")
        {
            Session["msg"] = "Lỗi! Mã nhận vào không hợp lệ!";
            Session["msg_type"] = "error";
            Response.Redirect("TrangChu.aspx");
        }
        // SqlDataSource1.SelectCommand = "SELECT [TenTL], [TacGia], [MoTa], [HinhAnh], [DonGia], FROM [TaiLieu] WHERE IDLoaiTL=" + ma_loai;
        SqlDataSource1.SelectCommand = "SELECT *,(SELECT B2.TenLoaiTL FROM LoaiTaiLieu B2 WHERE B1.IDLoaiTL = B2.IDLoaiTL) TenLoaiTL FROM [TaiLieu] B1 WHERE IDTaiLieu=" + ma;
        if (!IsPostBack)
            ListView1.DataBind();
        
        SqlDataSource2.SelectCommand = "SELECT *,(SELECT B2.TenLoaiTL FROM LoaiTaiLieu B2 WHERE B1.IDLoaiTL = B2.IDLoaiTL) TenLoaiTL FROM [TaiLieu] B1 WHERE [IDLoaiTL]= (SELECT IDLoaiTL FROM [TaiLieu] WHERE IDTaiLieu = " + ma + ")";
        ListView2.DataSource = SqlDataSource2;
        ListView2.DataBind();
    }
    protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        if (String.Equals(e.CommandName, "AddToCard"))
        {

            ListViewDataItem dataItem = (ListViewDataItem)e.Item;
            //ListView lv = (ListView)sender;
            string IDTaiLieu =
              ListView1.DataKeys[dataItem.DisplayIndex].Value.ToString();
            //TextBox tb = (TextBox)lv.FindControl("So_luong_tb");
            TextBox tb = (TextBox)e.Item.FindControl("So_luong_tb");
            string sl = tb.Text;

            int soluong = System.Int32.Parse(sl);

            
            string ten = ListView1.DataKeys[dataItem.DisplayIndex].Values["TenTL"].ToString();
            string gia = ListView1.DataKeys[dataItem.DisplayIndex].Values["DonGia"].ToString();
            int dongia = System.Int32.Parse(gia.Replace(".", ""));
            int ID = System.Int32.Parse(IDTaiLieu);
            long thanh_tien = dongia * soluong;
            //ShoppingCart tl = new ShoppingCart(ID,ten,dongia,soluong,thanh_tien);

            //Session.Add("gio_hang",tl);

            ShoppingCart card = (ShoppingCart)Session["gio_hang"];
            if (card == null)
                card = new ShoppingCart();
            card.Add(ID, ten, dongia, soluong, thanh_tien);
            Session["gio_hang"] = card;

            ASP.masterpage_master master = (ASP.masterpage_master)this.Master;
            master.RefreshShoppingCartInfo();
            /*
            if (SelectedEmployeesListBox.Items.FindByValue(IDTaiLieu) == null)
            {
                ListItem item = new ListItem(e.CommandArgument.ToString(), IDTaiLieu);
                SelectedEmployeesListBox.Items.Add(item);
            }
             */
        }
    }

    public string format_tt(string tt)
    {

        string rs = tt;
        //thay dau ',' thanh '.'
        int l = (rs.Length - 1) / 3;
        for (int i = 0; i < l; i++)
        {
            rs = rs.Insert(rs.Length - (i + 1) * 3 - i, ".");
        }

        return rs;
    }
}